﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditCustomer : BaseForm
    {
        public string ID = string.Empty;
        public RoomInfo roomInfo = null;
        private OrderInfo orderInfo = null;
        public string action = "";

        public FrmEditCustomer()
        {
            InitializeComponent();
        }

        private void SetInfo(CustomerInfo info)
        {
            info.Address = txtAddress.Text;
            info.CompanyName = txtCompany.Text;
            info.IDCarType = cmbIDCarType.Text;
            info.Name = txtName.Text;
            info.Note = txtNote.Text;
            info.IDNumber = txtIDNumber.Text;
            info.Telephone = txtTelephone.Text;
            info.Source = cmbSource.Text;
            info.CustomerType = cmbType.Text;
            info.Sex = cmbSex.Text;            
        }

       

        private void InitDictItem()
        {
            this.cmbSource.Items.Clear();
            this.cmbSource.Items.AddRange(DictItemUtil.GetCustomerSource());

            this.cmbType.Items.Clear();
            this.cmbType.Items.AddRange(DictItemUtil.GetCustomerType());

            this.cmbIDCarType.Items.Clear();
            this.cmbIDCarType.Items.AddRange(DictItemUtil.GetIDCarType());
        }

        private void FrmEditCustomer_Load(object sender, EventArgs e)
        {
           

            InitDictItem();

            if (roomInfo != null)
            {

                if (action == "offteam")
                {
                    this.Text = "脱团操作";
                    btnOK.Text = "脱离团队";
                }
                else
                {

                    this.Text = "修改团队队员信息";
                   // btnOK.Text = "b";
                
                }
                orderInfo = BLLFactory<Order>.Instance.GetCurrentOrder(roomInfo.RoomNo);
                ID = orderInfo.Customer_ID.ToString();
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
                if (info != null)
                {
                    ShowCostomerInfo(info);
                }
                else
                {

                    txtName.Text = "";
                    txtIDNumber.Text = "";

                }



            }
            else
            {
                if (!string.IsNullOrEmpty(ID))
                {
                    this.Text = "编辑 " + this.Text;
                    CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
                    if (info != null)
                    {
                        ShowCostomerInfo(info);
                    }

                    //this.btnSaveAndOrder.Enabled = Portal.gc.HasFunction("Customer/Modify");
                    //this.btnOK.Enabled = Portal.gc.HasFunction("Customer/Modify");
                }
                else
                {
                    this.Text = "新建 " + this.Text;
                    lblCreateDate.Text = DateTime.Now.ToString();

                    //this.btnSaveAndOrder.Enabled = Portal.gc.HasFunction("Customer/Add");
                    //this.btnOK.Enabled = Portal.gc.HasFunction("Customer/Add");
                }

            }

            //this.btnNewOrder.Enabled = Portal.gc.HasFunction("Order/Add");
        }

        private void ShowCostomerInfo(CustomerInfo info)
        {
            #region 显示客户信息
            txtAddress.Text = info.Address;
            txtCompany.Text = info.CompanyName;
            txtName.Text = info.Name;
            txtNote.Text = info.Note;
            txtIDNumber.Text = info.IDNumber;
            txtTelephone.Text = info.Telephone;
            cmbSource.Text = info.Source;
            cmbType.Text = info.CustomerType;
            cmbSex.Text = info.Sex;
            cmbIDCarType.Text = info.IDCarType;
            lblCreateDate.Text = info.RegistrationDate.ToString();

            #endregion
        }

        private void btnNewOrder_Click(object sender, EventArgs e)
        {
            //FrmEditOrder dlg = new FrmEditOrder();
            //dlg.CustomerNumber = this.txtNumber.Text;
            //dlg.ShowDialog();
        }

        private void btnSaveAndOrder_Click(object sender, EventArgs e)
        {
            
            
            SaveData(true);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveData(false);
        }

        private void SaveData(bool isCreateOrder)
        {
            #region 输入验证
            if (this.txtIDNumber.Text.Length == 0)
            {
                MessageUtil.ShowTips("证件号码不能为空");
                this.txtIDNumber.Focus();
                return;
            }
            else if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("宾客名称不能为空");
                this.txtName.Focus();
                return;
            }

            if (cmbIDCarType.Text == "身份证")
            {

                if (!Public.CheckIDCard(txtIDNumber.Text))
                {

                    MessageUtil.ShowTips("身份证号码不正确");
                    this.txtIDNumber.Focus();
                    return;

                }



            }
            #endregion

            if (roomInfo != null)//脱离团队
            {

                #region 新建脱离团队成员信息
                CustomerInfo info = new CustomerInfo();
                SetInfo(info);
                info.RegistrationDate = DateTime.Now;

                try
                {
                    CustomerInfo existInfo = BLLFactory<Customer>.Instance.CheckUser(txtName.Text, this.txtIDNumber.Text);
                    if (existInfo == null)
                    {
                        bool succeed = BLLFactory<Customer>.Instance.Insert(info);
                        if (succeed)
                        {

                            existInfo = BLLFactory<Customer>.Instance.CheckUser(txtName.Text, this.txtIDNumber.Text);
                           


                        }

                    }
                    if (existInfo != null)
                    {

                        if (action == "offteam")
                        {
                            orderInfo.Note = "脱离团队:" + orderInfo.TeamName;
                            orderInfo.TeamName = "";
                            orderInfo.CheckType = 0;
                            orderInfo.MasterRoomNo = roomInfo.RoomNo;
                            orderInfo.OrderNo = Public.GetOrderNumber();
                            orderInfo.BillNumber = orderInfo.OrderNo;
                        }
                        
                        orderInfo.IDNumber=existInfo.IDNumber;
                        orderInfo.Customer_ID=existInfo.ID;
                        orderInfo.CustomerName=existInfo.Name;
                       
                        bool sucess= BLLFactory<Order>.Instance.Update(orderInfo, orderInfo.ID.ToString());
                        if (sucess)
                        {
                            if (action == "offteam")
                            {
                                MessageUtil.ShowTips("脱离团队操作成功");

                            }
                            else
                            {

                                MessageUtil.ShowTips("修改团队队员信息成功");
                            }
                            this.DialogResult = DialogResult.OK;

                        }
                        else
                        {
                            if (action == "offteam")
                            {
                                MessageUtil.ShowTips("脱离团队操作失败");

                            }
                            else
                            {

                                MessageUtil.ShowTips("修改团队队员信息失败");
                            }
                           
                        
                        }

                       

                    }
                    else
                    {

                        MessageUtil.ShowTips("脱离团队操作失败！");
                    
                    
                    }
                   
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
                #endregion

            }
            else
            {


                if (!string.IsNullOrEmpty(ID))
                {
                    CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
                    if (info != null)
                    {
                        SetInfo(info);

                        try
                        {
                            bool succeed = BLLFactory<Customer>.Instance.Update(info, info.ID.ToString());
                            if (succeed)
                            {
                                MessageUtil.ShowTips("保存成功");
                                this.DialogResult = DialogResult.OK;

                                if (isCreateOrder)
                                {
                                    //FrmEditOrder frmOrder = new FrmEditOrder();
                                    //frmOrder.CustomerNumber = info.Number;
                                    //frmOrder.Show();
                                }
                                this.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                            MessageUtil.ShowError(ex.Message);
                        }
                    }
                }
                else
                {
                    #region 新建保存
                    CustomerInfo info = new CustomerInfo();
                    SetInfo(info);
                    info.RegistrationDate = DateTime.Now;

                    try
                    {
                        bool succeed = BLLFactory<Customer>.Instance.Insert(info);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;

                            if (isCreateOrder)
                            {
                                //FrmEditOrder frmOrder = new FrmEditOrder();
                                //frmOrder.CustomerNumber = info.Number;
                                //frmOrder.Show();
                            }
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                    #endregion
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
